package app.com.freshmenapp3;

import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.sun.istack.internal.Nullable;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by Administrator on 2020/9/28.
 */

public class RegistActivity  extends AppCompatActivity{

    private EditText nameEd;
    private EditText usernameEd;
    private EditText emailEd;
    private EditText telephoneEd;
    private EditText passwordEd;
    private EditText repasswordEd;
    private Spinner departmentSp;
    private Spinner majorSp;
    private Button registBtn;
    private String departmentStr;
    private String majorStr;
    private RequestQueue requestQueue;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_regist);
        initView();
        initEvent();
    }

    private void initView(){
        nameEd = findViewById(R.id.regist_name_ed);
        usernameEd = findViewById(R.id.regist_username_ed);
        emailEd = findViewById(R.id.regist_email_ed);
        telephoneEd = findViewById(R.id.regist_telephone_ed);
        passwordEd = findViewById(R.id.regist_password_ed);
        repasswordEd = findViewById(R.id.regist_repassword_ed);

        departmentSp = findViewById(R.id.regist_department_sp);
        majorSp = findViewById(R.id.regist_major_sp);

        registBtn = findViewById(R.id.regist_btn);
    }

    private void initEvent(){
        //获取部门下拉框选择框数据
        departmentSp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                String[] departmentArray = getResources().getStringArray(R.array.departmentarray);
                departmentStr = departmentArray[i].toString();
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });
        //获取专业下拉选择框数据
        majorSp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                String[] majorArray = getResources().getStringArray(R.array.majorarray);
                majorStr = majorArray[i].toString();
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });
        //注册按钮点击事件
        registBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                initData();//调用发送网络请求方法
            }
        });
    }

    private void initData(){
        String name = nameEd.getText().toString();
        String username = usernameEd.getText().toString();
        String email = emailEd.getText().toString();
        String telephone = telephoneEd.getText().toString();
        String password = passwordEd.getText().toString();
        String repassword = repasswordEd.getText().toString();
        if (password.equals(repassword)){
            JSONObject jsonObject = new JSONObject();
            try {
                jsonObject.put("name",name);
                jsonObject.put("username",username);
                jsonObject.put("email",email);
                jsonObject.put("telephone",telephone);
                jsonObject.put("password",password);
                jsonObject.put("department",departmentStr);
                jsonObject.put("major",majorSp);
                jsonObject.put("role","学生");
            }catch (JSONException e){
                e.printStackTrace();
            }
            RequestQueue requestQueue = Volley.newRequestQueue(RegistActivity.this);
            String url="http://106.12.30.77:8080/freshmenapp/user/regist";
            JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url, jsonObject, new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject jsonObject) {
                    Toast.makeText(RegistActivity.this, jsonObject.optString("msg"), Toast.LENGTH_SHORT).show();
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError volleyError) {
                    Toast.makeText(RegistActivity.this,"网络异常",Toast.LENGTH_SHORT).show();
                }
            });
            requestQueue.add(jsonObjectRequest);
        }else {
            Toast.makeText(this,"密码和重复密码不一致",Toast.LENGTH_SHORT).show();
        }


    }
}
